/*
 * @Author: your name
 * @Date: 2021-06-26 20:20:30
 * @LastEditTime: 2021-06-28 19:06:25
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \LinkList\752\752.cpp
 */
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <iostream>
#include <unordered_set>
#include <chrono>
#include <unordered_set>
#define DEBUG ;
using namespace std;
using namespace chrono;
class Solution
{
public:
    int openLock(vector<string> &deadends, string target)
    {

        unordered_set<string> dead;
        for (auto i : deadends)
            dead.insert(i);
        string str = "0000";
        queue<string> que;
        que.push(str);
        int trial = 0;
#if defined(DEBUG)

        auto start = system_clock::now();

#endif // DEBUG

        while (que.size() != 0)
        {

            int sz = que.size();
            for (int j = 0; j < sz; ++j)
            {
                auto nd = que.front();
                que.pop();
                if (dead.count(nd))
                    continue;
                if(nd==target)
                    return trial;
                dead.insert(nd);

                for (int i = 0; i < 4; ++i)
                {

                    if (nd[i] == '9')
                        nd[i] = '0';
                    else
                        nd[i] += 1;

                    if (!dead.count(nd))
                    {

                        if (target == nd)
                            return trial + 1;
                        else
                            que.push(nd);
                    }

                    //这里很重要！
                    if (nd[i] == '0')
                        nd[i] = '9';
                    else
                        nd[i] = nd[i] - 1;

                    if (nd[i] == '0')
                        nd[i] = '9';
                    else
                        nd[i] = nd[i] - 1;

                    if (!dead.count(nd))
                    {
                        if (target == nd)
                            return trial + 1;
                        else
                            que.push(nd);
                    }

                    if (nd[i] == '9')
                        nd[i] = '0';
                    else
                        nd[i] = nd[i] + 1;
                }
            }
#if defined(DEBUG)

            auto end = system_clock::now();
            auto duration = duration_cast<microseconds>(end - start);
            cout << "While Spent" << double(duration.count()) * microseconds::period::num / microseconds::period::den << " seconds." << endl;

#endif // DEBUG
            trial++;
        }
        return -1;
    }
};

class Solution2
{
public:
    int openLock(vector<string> &deadends, string target)
    {
        if (target == "0000")
        {
            return 0;
        }

        unordered_set<string> dead(deadends.begin(), deadends.end());
        if (dead.count("0000"))
        {
            return -1;
        }

        auto num_prev = [](char x) -> char
        {
            return (x == '0' ? '9' : x - 1);
        };
        auto num_succ = [](char x) -> char
        {
            return (x == '9' ? '0' : x + 1);
        };

        // 枚举 status 通过一次旋转得到的数字
        auto get = [&](string &status) -> vector<string>
        {
            vector<string> ret;
            for (int i = 0; i < 4; ++i)
            {
                char num = status[i];
                status[i] = num_prev(num);
                ret.push_back(status);
                status[i] = num_succ(num);
                ret.push_back(status);
                status[i] = num;
            }
            return ret;
        };

        queue<pair<string, int>> q;
        q.emplace("0000", 0);
        unordered_set<string> seen = {"0000"};

        while (!q.empty())
        {
            auto [status, step] = q.front();
            q.pop();
            for (auto &&next_status : get(status))
            {
                if (!seen.count(next_status) && !dead.count(next_status))
                {
                    if (next_status == target)
                    {
                        return step + 1;
                    }
                    q.emplace(next_status, step + 1);
                    seen.insert(move(next_status));
                }
            }
        }
        return -1;
    }
};
class Solution3 {
public:
    int openLock(vector<string>& deadends, string target) {
      unordered_set<string> deads;
     for (string s :deadends) deads.insert(s);
     queue<string> q;
     int step =0;
     q.push("0000");

     while(q.size()!=0)
     {
         int sz =q.size();
         for (int i =0;i<sz;++i)
         {
             string cur =q .front();
             q.pop();
             if(deads.count(cur))
             continue;
             if(cur==target)
             return step;
            deads.insert(cur);

             for (int j =0;j<4;++j)

             {
                string up = plusOne(cur,j);
                 if(!deads.count(up))
                 {
                     if(up==target)
                     return step+1;
                     else
                     q.push(up);

                 }
                string down = minusOne(cur,j);
                if(!deads.count(down))
                {
                    if(down==target)
                    return step+1;
                   else  q.push(down);
                }
             }
         }
         step++;
     }
     return-1;
}
string plusOne(string s,int i)
{
    s[i]=s[i]=='9'?'0':s[i]+1;
    return s ;
}

string minusOne(string s ,int i )
{
    s[i]=s[i]=='0'?'9':s[i]-1;
    return s ;
}

};
int main()
{
    vector<string> dead = {"8887", "8889", "8878", "8898", "8788", "8988", "7888", "9888"};
    string target = "8888";
    Solution s1;

    auto start = system_clock::now();
    // do something...

    cout << s1.openLock(dead, target);
    auto end = system_clock::now();
    auto duration = duration_cast<microseconds>(end - start);
    cout << "Spent" << double(duration.count()) * microseconds::period::num / microseconds::period::den << " seconds." << endl;
}